package com.xiliu.server.common.wxapp.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiliu.common.constant.WxConstants;
import com.xiliu.server.common.wxapp.entity.WxappProperties;
import com.xiliu.server.modules.wxmsg.entity.XlWxPublicInfo;
import com.xiliu.server.modules.wxmsg.service.IXlWxPublicInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author xiliu
 *  微信小程序配置
 * @date 2022/12/27 9:28
 */
@Configuration
public class WxappConfiguration {

    @Autowired
    private IXlWxPublicInfoService wxPublicInfoService;

    @Bean
    public WxMaService wxMaService() {
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        XlWxPublicInfo wxPublicInfo = wxPublicInfoService.getOne(new LambdaQueryWrapper<XlWxPublicInfo>()
                .eq(XlWxPublicInfo::getPlatformType, WxConstants.TYPE_WXAPP),false);
        config.setAppid(wxPublicInfo.getAppId());
        config.setSecret(wxPublicInfo.getAppSecret());
        config.setMsgDataFormat(wxPublicInfo.getMsgDataFormat());

        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(config);
        return service;
    }
}
